﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace 易容酒店管理系统
{
    public partial class Finance : Form
    {
        public Finance()
        {
            InitializeComponent();
        }

        private void picReturn_Click(object sender, EventArgs e)
        {
            
        }

        private void Finance_Load(object sender, EventArgs e)
        {
            calculate();
            Quarter();
            Month();
            Lookup();
        }
        //查看该月收入
        public void LookMonth(string ID)
        {
            listView1.Items.Clear();
            string Link = "Server=.;Database=Management_system;integrated security=SSPI;";
            SqlConnection con = new SqlConnection(Link);
            string Sql = "select * from Finance where PrimaryID='" + ID + "';";
            SqlDataAdapter Add = new SqlDataAdapter(Sql, con);
            DataSet set = new DataSet();
            Add.Fill(set, "Finance");
            DataTable table = set.Tables["Finance"];
            for (int i = 0; i < table.Rows.Count; i++)
            {
                DataRow drow = table.Rows[i];
                ListViewItem lvi = new ListViewItem(drow[0].ToString());
                lvi.SubItems.Add(drow[0].ToString());
                lvi.SubItems.Add(drow[1].ToString());
                lvi.SubItems.Add(drow[2].ToString());
                lvi.SubItems.Add(drow[3].ToString());
                lvi.SubItems.Add(drow[4].ToString());
                lvi.SubItems.Add(drow[5].ToString());
                lvi.SubItems.Add(drow[6].ToString());
                lvi.SubItems.Add(drow[7].ToString());
                listView1.Items.Add(lvi);
            }
        }
        //查询
        private void button1_Click(object sender, EventArgs e)
        {
            calculate();
            Quarter();
            Month();
            Lookup();
        }
        //查询
        private void Lookup()
        {
            string ID = "";
            DateTime Time = dateTimePicker1.Value;
            int Year = Convert.ToInt32(Time.ToString("yyyy"));
            int Month = Convert.ToInt32(Time.ToString("MM"));
            int Day = Convert.ToInt32(Time.ToString("dd"));
            if (Day > 10)
            {
                if (Day > 10)
                {
                    ID = Year.ToString() + Month.ToString() + Day.ToString();
                }
                else
                {
                    ID = Year.ToString() + Month.ToString() + "0" + Day.ToString();
                }
            }
            else
            {
                if (Day > 10)
                {
                    ID = Year.ToString() + "0" + Month.ToString() + Day.ToString();
                }
                else
                {
                    ID = Year.ToString() + "0" + Month.ToString() + "0" + Day.ToString();
                }
            }

            listView1.Items.Clear();
            string Link = "Server=.;Database=Management_system;integrated security=SSPI;";
            SqlConnection con = new SqlConnection(Link);
            string Sql = "select * from Finance where PrimaryID='" + ID + "';";
            SqlDataAdapter Add = new SqlDataAdapter(Sql, con);
            DataSet set = new DataSet();
            Add.Fill(set, "Finance");
            DataTable table = set.Tables["Finance"];
            for (int i = 0; i < table.Rows.Count; i++)
            {
                DataRow drow = table.Rows[i];
                ListViewItem lvi = new ListViewItem(drow[0].ToString());
                lvi.SubItems.Add(drow[0].ToString());
                lvi.SubItems.Add(drow[1].ToString());
                lvi.SubItems.Add(drow[2].ToString());
                lvi.SubItems.Add(drow[3].ToString());
                lvi.SubItems.Add(drow[4].ToString());
                lvi.SubItems.Add(drow[5].ToString());
                lvi.SubItems.Add(drow[6].ToString());
                lvi.SubItems.Add(drow[7].ToString());
                listView1.Items.Add(lvi);
            }
        }
        //计算
        public void calculate()
        {
            string Link = "Server=.;Database=Management_system;integrated security=SSPI;";
            using (SqlConnection con = new SqlConnection(Link))
            {
                DateTime Time = dateTimePicker1.Value;
                int Month = Convert.ToInt32(Time.ToString("MM"));
                string Sql = "select sum(InMoney) from Finance where PrimaryID like'_____[" + Month.ToString() + "]%';";
                using (SqlCommand cmd = new SqlCommand(Sql, con))
                {
                    con.Open();
                    label2.Text = cmd.ExecuteScalar().ToString();
                    con.Close();
                }


            }



        }
        //季度
        public void Quarter()
        {
            string Link = "Server=.;Database=Management_system;integrated security=SSPI;";
            DateTime Time = dateTimePicker1.Value;
            int Months = Convert.ToInt32(Time.ToString("MM"));

            using (SqlConnection con = new SqlConnection(Link))
            {
                if (Months >= 1 && Months <= 3)
                {
                    string Sql2 = "select sum(InMoney) from Finance where PrimaryID like'_____[1 2 3 ]%';";
                    using (SqlCommand cmd2 = new SqlCommand(Sql2, con))
                    {
                        con.Open();
                        label3.Text = cmd2.ExecuteScalar().ToString();
                        con.Close();
                    }
                }
                else if (Months >= 4 && Months <= 6)
                {
                    string Sql2 = "select sum(InMoney) from Finance where PrimaryID like'_____[4 5 6]%';";
                    using (SqlCommand cmd2 = new SqlCommand(Sql2, con))
                    {
                        con.Open();
                        label3.Text = cmd2.ExecuteScalar().ToString();
                        con.Close();
                    }
                }
                else if (Months >= 7  && Months <= 9)
                {
                    string Sql2 = "select sum(InMoney) from Finance where PrimaryID like'_____[7 8 9]%';";
                    using (SqlCommand cmd2 = new SqlCommand(Sql2, con))
                    {
                        con.Open();
                        label3.Text = cmd2.ExecuteScalar().ToString();
                        con.Close();
                    }
                }
                else if (Months >= 10&& Months <= 12)
                {
                    string Sql2 = "select sum(InMoney) from Finance where PrimaryID like'_____[10 11 12]%';";
                    using (SqlCommand cmd2 = new SqlCommand(Sql2, con))
                    {
                        con.Open();
                        label3.Text = cmd2.ExecuteScalar().ToString();
                        con.Close();
                    }
                }

            }
        }
        //月份
        public void Month()
        {
            string Link = "Server=.;Database=Management_system;integrated security=SSPI;";
            using (SqlConnection con = new SqlConnection(Link))
            {
                DateTime Time = dateTimePicker1.Value;
                int Month = Convert.ToInt32(Time.ToString("MM"));
                string Sql3 = "select sum(InMoney) from Finance where PrimaryID like'_____[" + Month.ToString() + "]%';";
                using (SqlCommand cmd3 = new SqlCommand(Sql3, con))
                {
                    con.Open();
                    label6.Text = cmd3.ExecuteScalar().ToString();
                    con.Close();
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Main_function newzMain_function = new Main_function();
            newzMain_function.Show();
            newzMain_function.TopLevel = false;
            Method.frm.panel1.Controls.Clear();
            Method.frm.panel1.Controls.Add(newzMain_function);
            Method.SetDouble(Method.frm);
        }
    }
}
